﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace monitor_server_2.CustomControls
{
    public partial class CoordinatesSetter : UserControl
    {
        private ContextMenuStrip _menu;
        private bool _readOnly = true;

        public Color DestTextColor
        {
            get { return destinationLabel.ForeColor; }
            set { destinationLabel.ForeColor = value; }
        }
        public Color DestBackColor
        {
            get { return destinationLabel.BackColor; }
            set { destinationLabel.BackColor = value; }
        }

        public Color CoordinatesBackColor
        {
            get { return BackColor; }
            set { BackColor = value; }
        }

        public string DestText
        {
            get { return destinationLabel.Text; }
            set
            {
                if (value == "E" || value == "N" || value == "W" || value == "S")
                destinationLabel.Text = value;

                switch (value)
                {
                    case "N":
                        DestBackColor = Color.Aqua;
                        break;
                    case "W":
                        DestBackColor = Color.Yellow;
                        break;
                    case "S":
                        DestBackColor = Color.GhostWhite;
                        break;
                    case "E":
                        DestBackColor = Color.GreenYellow;
                        break;
                    default:
                        return;
                }
                destinationLabel.Text = value;
            }
        }

        public string Coordinates
        {
            get
            {
                var ret = "";
                if (degreesBox.Value < 10)
                    ret += "0" + degreesBox.Value;
                else
                    ret += degreesBox.Value;
                if (minutesBox.Value < 10)
                    ret += "0" + minutesBox.Value;
                else
                    ret += minutesBox.Value;
                if (secondsBox.Value < 10)
                    ret += "0" + secondsBox.Value;
                else
                    ret += secondsBox.Value;

                return ret;
            }
            set
            {
                if (value.Length != 6)
                    return;
                degreesBox.Text = value.Substring(0, 2);
                minutesBox.Text = value.Substring(2, 2);
                secondsBox.Text = value.Substring(4, 2);
            }
        }

        public decimal MaxDegrees
        {
            get { return degreesBox.Maximum; }
            set { degreesBox.Maximum = value; }

        }
        public decimal MinDegrees
        {
            get { return degreesBox.Minimum; }
            set { degreesBox.Minimum = value; }

        }

        public bool ReadOnly
        {
            get { return !_readOnly; }
            set
            {
                _readOnly = !value;
                degreesBox.Enabled = !value;
                minutesBox.Enabled = !value;
                secondsBox.Enabled = !value;
            }
        }

        public CoordinatesSetter()
        {
            InitializeComponent();
            
            //degrees
            degreesBox.Maximum = 90;
            degreesBox.Minimum = 0;

            //minutes
            minutesBox.Maximum = 60;
            minutesBox.Minimum = 0;

            //seconds
            secondsBox.Maximum = 60;
            secondsBox.Minimum = 0;

        }

        public bool SetDestinations(params string[] destinations)
        {
            if (destinations.Length > 4)
            {
                return false;
            }
            _menu.Items.Clear();
            foreach (var dest in destinations)
            {
                switch (dest)
                {
                    case "N":
                        _menu.Items.Add("N");
                        break;
                    case "W":
                        _menu.Items.Add("W");
                        break;
                    case "S":
                        _menu.Items.Add("S");
                        break;
                    case "E":
                        _menu.Items.Add("E");
                        break;
                    default:
                        return false;
                }
            }
            return true;
        }

        private void CoordinatesSetter_Load(object sender, EventArgs e)
        {
            _menu = new ContextMenuStrip();
            _menu.Items.Add("N");
            _menu.Items.Add("W");
            _menu.Items.Add("S");
            _menu.Items.Add("E");
            _menu.ItemClicked += _menu_ItemClicked;
        }

        void _menu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            DestText = e.ClickedItem.Text;
            
        }

        private void destinationLabel_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right || ReadOnly)
                return;
            _menu.Show(MousePosition);
        }
    }
}
